<?php
require_once("creat_smpicture.php");

$type=trim($_GET['type']);
if($type=='1'){                      //产品
	$targetFolder = '/uploadfile/product/'; // 保存的文件夹
	$n_name=date('YmdHis') . mt_rand(100, 999);
	img_upload($FILE,$targetFolder,$n_name,$is_thumb='true');
}else if($type=='2'){     	         //文章			
	$targetFolder = '/uploadfile/article/';
	$n_name=date('YmdHis') . mt_rand(100, 999);
	img_upload($FILE,$targetFolder,$n_name,$is_thumb='true');
}else if($type=='3'){     	         //广告图片		
	$targetFolder = '/uploadfile/img/';
	img_upload($FILE,$targetFolder);
}else if($type=='4'){     	         //友情链接图片	
	$targetFolder = '/uploadfile/link/';
	img_upload($FILE,$targetFolder);
}




/*
* $targetFolder:目标目录
* $n_ame :新文件名字，为空则用旧名字
* $is_thumb ：为true生成缩略图		
*/
function img_upload($FILE,$targetFolder,$n_name='',$is_thumb='false'){
	$tempFile = $_FILES['Filedata']['tmp_name'];
	$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
	if(!is_dir($targetPath)){         //如果文件夹不存在，则创建
	 	@mkdir($targetPath,0777);
	} 
	$fileTypes = array('jpg','jpeg','gif','png','pptx','doc'); // 允许上传的类型
	$fileParts = pathinfo($_FILES['Filedata']['name']);
	if (in_array(strtolower($fileParts['extension']),$fileTypes)) {
		if($n_name!=''){ //是否新名字
			$newname = rtrim($targetPath,'/') . '/'. $n_name . '.' . $fileParts['extension'];//该文件名字
		}else{
			$newname = rtrim($targetPath,'/') . '/'.$_FILES['Filedata']['name'];//该文件名字
		}
		
		$newnames=$targetFolder . basename($newname);
		move_uploaded_file($tempFile,iconv("UTF-8","gb2312", $newname));//上传
		
		if($is_thumb=='true'){
			$thumb=new Image();	//生成略缩图
			$thumbpath='../..'.rtrim($targetFolder,'/') . '/thumb/';
			if(!file_exists($thumbpath)){  //不存在则创建
				mkdir($thumbpath,0777);
			}
			$thumbname=$thumbpath. basename($newname);
			$thumbname1=$targetFolder . '/thumb/'. basename($newname);
			$newnames1='../..'.$newnames;
			$thumb->thumb($newnames1,$thumbname);
		}
		echo $newnames ;
		//echo $fileParts;
	} else {
		echo "type is wrong";
	}
}
?>